﻿using System.Linq.Expressions;
using IRepositorys;
using IServices;
using SqlSugar;

namespace Services;

public class BaseService<T> : IBaseService<T>
    where T : class, new()
{
    private readonly IBaseRepository<T> _repository;

    public BaseService(IBaseRepository<T> repository)
    {
        _repository = repository;
    }

    public bool Add(T entity)
    {
        return _repository.Insert(entity);
    }

    public async Task<bool> AddAsync(T entity)
    {
        return await _repository.InsertAsync(entity);
    }

    public bool AddRange(List<T> entities)
    {
        return _repository.InsertRange(entities);
    }

    public async Task<bool> AddRangeAsync(List<T> entities)
    {
        return await _repository.InsertRangeAsync(entities);
    }

    public bool Delete(T entity)
    {
        return _repository.Delete(entity);
    }

    public async Task<bool> DeleteAsync(T entity)
    {
        return await _repository.DeleteAsync(entity);
    }

    public bool Delete(List<T> entities)
    {
        return _repository.Delete(entities);
    }

    public async Task<bool> DeleteAsync(List<T> entities)
    {
        return await _repository.DeleteAsync(entities);
    }

    public bool Delete(Expression<Func<T, bool>> predicate)
    {
        return _repository.Delete(predicate);
    }

    public async Task<bool> DeleteAsync(Expression<Func<T, bool>> predicate)
    {
        return await _repository.DeleteAsync(predicate);
    }

    public bool DeleteById(object id)
    {
        return _repository.DeleteById(id);
    }

    public async Task<bool> DeleteByIdAsync(object id)
    {
        return await _repository.DeleteByIdAsync(id);
    }

    public bool DeleteByIds(dynamic[] ids)
    {
        return _repository.DeleteByIds(ids);
    }

    public async Task<bool> DeleteByIdsAsync(dynamic[] ids)
    {
        return await _repository.DeleteByIdsAsync(ids);
    }

    public T GetById(dynamic id)
    {
        return _repository.GetById(id);
    }

    public async Task<T> GetByIdAsync(dynamic id)
    {
        return await _repository.GetByIdAsync(id);
    }

    public IEnumerable<T> GetList()
    {
        return _repository.GetList();
    }

    public IEnumerable<T> GetList(Expression<Func<T, bool>> predicate)
    {
        return _repository.GetList(predicate);
    }

    public async Task<IEnumerable<T>> GetListAsync()
    {
        return await _repository.GetListAsync();
    }

    public async Task<IEnumerable<T>> GetListAsync(Expression<Func<T, bool>> predicate)
    {
        return await _repository.GetListAsync(predicate);
    }

    public async Task<IEnumerable<T>> GetListAsync(
        Expression<Func<T, bool>> predicate,
        dynamic selectColumns
    )
    {
        return await _repository.GetListAsync(predicate, selectColumns);
    }

    public IEnumerable<T> GetPageList(
        Expression<Func<T, bool>> expression,
        int pageIndex,
        int pageSize
    )
    {
        return _repository.GetPageList(
            expression,
            new PageModel() { PageIndex = pageIndex, PageSize = pageSize }
        );
    }

    public IEnumerable<T> GetPageList(
        Expression<Func<T, bool>> expression,
        int pageIndex,
        int pageSize,
        Expression<Func<T, object>> orderByExpression,
        OrderByType orderByType = OrderByType.Asc
    )
    {
        return _repository.GetPageList(
            expression,
            new PageModel() { PageIndex = pageIndex, PageSize = pageSize },
            orderByExpression,
            orderByType
        );
    }

    public async Task<IEnumerable<T>> GetPageListAsync(
        Expression<Func<T, bool>> expression,
        int pageIndex,
        int pageSize
    )
    {
        return await _repository.GetPageListAsync(
            expression,
            new PageModel() { PageIndex = pageIndex, PageSize = pageSize }
        );
    }

    public async Task<IEnumerable<T>> GetPageListAsync(
        Expression<Func<T, bool>> expression,
        int pageIndex,
        int pageSize,
        Expression<Func<T, object>> orderByExpression,
        OrderByType orderByType = OrderByType.Asc
    )
    {
        return await _repository.GetPageListAsync(
            expression,
            new PageModel() { PageIndex = pageIndex, PageSize = pageSize },
            orderByExpression,
            orderByType
        );
    }

    public T GetSingle(Expression<Func<T, bool>> predicate)
    {
        return _repository.GetSingle(predicate);
    }

    public async Task<T> GetSingleAsync(Expression<Func<T, bool>> predicate)
    {
        return await _repository.GetSingleAsync(predicate);
    }

    public object InsertReturnIdentity(T entity)
    {
        return _repository.InsertReturnIdentity(entity);
    }

    public async Task<object> InsertReturnIdentityAsync(T entity)
    {
        return await _repository.InsertReturnIdentityAsync(entity);
    }

    public bool Update(T entity)
    {
        return _repository.Update(entity);
    }

    public async Task<bool> UpdateAsync(T entity)
    {
        return await _repository.UpdateAsync(entity);
    }

    public bool UpdateRange(List<T> entities)
    {
        return _repository.UpdateRange(entities);
    }

    public async Task<bool> UpdateRangeAsync(List<T> entities)
    {
        return await _repository.UpdateRangeAsync(entities);
    }
}
